import quotationRecordApi from '@/api/quotation/QuotationRecordApi.js'
import page from '@/mixins/page'

export default {
    name: "quotationRecord",
    mixins: [page],
    data () {
        return {
            // 参数
            params: {
                name: '',
                purpose: '',
                total: '',
                remark: '',
            },

            // 表单弹窗信息
            formDialog: {
                loading: false,
                title: '',
                state: '',
                visible: false,
            },
            // 表单数据
            formData: {
                name: '',
                purpose: '',
                remark: '',
            },
            detail: {
                visible: false,
                title: '详情',
                data: {},
            },
            rules: {
                name: [{required: true, message: "请输入名称", trigger: "blur"}],
                purpose: [{required: true, message: "请输入用途说明", trigger: "blur"}]
            },
        }
    },
    methods: {
        // 查询
        page () {
            this.table.loading = true
            this.$rq.query(quotationRecordApi.url, {
                ...this.params,
                ...this.sorting,
                pageSize: this.pagination.pageSize,
                pageOn: this.pagination.pageOn
            }).then(res => {
                if (res.data && Array.isArray(res.data.rows) && res.data.rows.length) {
                this.pagination.total = res.data.total
                this.table.data = res.data.rows
            } else {
                this.total = 0
                this.table.data = []
            }
            }).finally(() => {
                // 无论失败还是成功, 都执行的操作
                this.table.loading = false
            })
        },
        // 新增
        handleInsert () {
            this.formReset()
            this.formDialog.visible = true
            this.formDialog.title = '新增'
            this.formDialog.state = 'insert'
        },
        // 表单重置
        formReset () {
            if (this.$refs.form) {
                this.$refs.form.clearValidate()
                this.$refs.form.resetFields()
            }
        },
        // 设置表单
        formSet (data) {
            this.formData.id = data.id
            this.formData.name = data.name
            this.formData.purpose = data.purpose
            this.formData.remark = data.remark
        },
        // 编辑
        handleEdit ({row}) {
            this.formReset()
            this.formSet(row)
            this.$nextTick(() => {
                this.formDialog.title = '修改'
                this.formDialog.state = 'edit'
                this.formDialog.visible = true
            })
        },
        // 删除(批量)
        handleDel () {
            if (this.table.selectionRows.length) {
                let ids = this.table.selectionRows.map(e => e.id)
                this.$confirm('确认删除?', {
                    title: '提示',
                    confirmButtonText: '删除',
                    type: 'warning',
                    beforeClose: (action, instance, done) => {
                    if (action == 'confirm') {
                        instance.confirmButtonLoading = true
                        instance.confirmButtonText = '执行中...'
                        this.$rq.deleteByIds(quotationRecordApi.url, ids).then(res => {
                        this.$message.success('删除成功')
                        this.page()
                        }).finally(() => {
                            done()
                        })
                    } else {
                        done()
                    }
                }
            })
            } else {
            this.$message.warning('请至少选择一行')
            }
        },
        formSave () {
            this.$router.push({name: 'quotationRecordInsert', query: this.formData})
        },
        // 详情
        handleDetail ({row, column, $index}) {
            this.$router.push({path: `quotationRecordDetail/${row.id}/detail`})
        },
        // 关闭详情
        detailClose () {
            this.detail.visible = false
        },
        // 关闭表单
        formClose () {
            this.formDialog.visible = false
        }
    },
    mounted () {
        this.page()
    }
}
